package com.lcb.bio;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

/****
 * @Author:刘长柏
 * @Date: 2021-02-19 10:00
 * @Description:socket服务器端
 *****/
public class SocketServer {
    public static void main(String[] args) throws IOException {
        ServerSocket serverSocket = new ServerSocket(9000);
        while (true) {
            System.out.println("等待连接...");
            final Socket socket = serverSocket.accept();//阻塞方法
            System.out.println("有客户端连接进来了...");

            new Thread(new Runnable() {
                public void run() {
                    try {
                        handler(socket);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }).start();

        }
    }

    private static void handler(Socket socket) throws IOException {
        System.out.println("线程ID: " + Thread.currentThread().getId());

        byte[] bytes = new byte[1024];
        System.out.println("准备read...");
        //接收客户端的数据,阻塞方法,没有数据可读时就阻塞
        int read = socket.getInputStream().read(bytes);
        System.out.println("read完毕...");
        if (read != -1) {
            System.out.println("接收客户端的数据: " + new String(bytes, 0, read));
            System.out.println("线程ID: " + Thread.currentThread().getId());
        }

        socket.getOutputStream().write("HelloClient".getBytes());
        socket.getOutputStream().flush();
    }
}
